import 'package:flutter/material.dart';
import 'package:flutter_app/base/base_state.dart';
import 'package:flutter_app/contract/article_list_presenter.dart';
import 'package:flutter_app/model/article_resp.dart';
import 'package:flutter_app/pages/load_more_widget.dart';
import 'package:flutter_app/pages/not_found_page.dart';
import 'package:flutter_app/provider/common_provider.dart';
import 'package:flutter_app/res/colours.dart';
import 'package:flutter_app/routers/router_utils.dart';
import 'package:provider/provider.dart';

import 'list_article_item.dart';

class ArticleListPage extends BaseFulWidget {
  String title;
  String id;

  //
  ArticleListPage(this.title, this.id);

  @override
  State<StatefulWidget> createState() => _ArticleListPage();
}

class _ArticleListPage extends BaseState<ArticleListPresenter, ArticleListPage>
    implements IArticleListView {
  GlobalKey<RefreshIndicatorState> _indicatorKey = GlobalKey<RefreshIndicatorState>();
  CommonProvider _articleProvider = CommonProvider<ArticleBean>();
  int pageNum = 1;

  //滑动监听
  final ScrollController _scrollController = new ScrollController();

  //没有更多了
  bool noMore = true;

  @override
  void initState() {
    super.initState();
    //第一次进入加载数据
    presenter.requestArticles(widget.id, pageNum);
    //添加滑动监听
    _scrollController.addListener(() {
      // 如果下拉的当前位置到scroll的最下面
      if (_scrollController.position.pixels ==
          _scrollController.position.maxScrollExtent) {
        pageNum++;
        presenter.requestArticles(widget.id, pageNum);
      }
    });
  }

  @override
  void dispose() {
    super.dispose();
    _scrollController.dispose();
  }

  @override
  Widget build(BuildContext context) => ChangeNotifierProvider(
        create: (_) => _articleProvider,
        child: Scaffold(
            backgroundColor: Colours.fff1f3f4,
            appBar: AppBar(
                backgroundColor: Colors.white,
                centerTitle: true,
                title: Text(
                  widget.title,
                  style: TextStyle(color: Colours.ff2c2c2c, fontSize: 16),
                ),
                leading: IconButton(
                    icon: Icon(
                      Icons.arrow_back,
                      color: Colours.ff0692ff,
                    ),
                    onPressed: () {
                      RouterUtils.goBack(context);
                    })),
            body: Consumer<CommonProvider>(
              builder: (context, provider, widget) {
                return RefreshIndicator(
                  key: _indicatorKey,
                  child: ListView.separated(
                    separatorBuilder: (BuildContext context, int index) =>
                        Divider(height: 10, color: Colours.fff1f3f4),

                    ///保持ListView任何情况都能滚动，解决在RefreshIndicator的兼容问题。
                    physics: const AlwaysScrollableScrollPhysics(),
                    itemBuilder: (context, index) {
                      if (_articleProvider.list.isEmpty) {
                        return NotFoundPage();
                      }
                      if (index == _articleProvider.list.length - 1) {
                        return LoadMoreWidget(noMore);
                      } else {
                        return ListArticleItem(_articleProvider.list[index]);
                      }
                    },
                    ///根据状态返回绘制 item 数量
                    itemCount: _articleProvider.list.length,
                    ///滑动监听
                    controller: _scrollController,
                  ),
                  onRefresh: _refresh,
                  color: Colours.ff0692ff, //指示器颜色
                );
              },
            )),
      );

  @override
  ArticleListPresenter createPresenter() => ArticleListPresenter(this);

  // 下拉加载的事件，清空之前list内容，取前X个
  // 其实就是列表重置
  Future<Null> _refresh() {
    pageNum = 1;
    presenter.requestArticles(widget.id, pageNum);
    return null;
  }

  @override
  void callBackArticles(List<ArticleBean> list) {
    if(pageNum == 1){
      _articleProvider.setDataList(list);
    } else {
      _articleProvider.addDataList(list);
    }
  }
}
